package com.chongbangbang.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.chongbangbang.pojo.PetInfo;
import com.chongbangbang.pojo.UserInfo;
import com.chongbangbang.service.PetInfoService;
import com.chongbangbang.service.UserInfoService;
import com.chongbangbang.service.WXService;
import com.chongbangbang.util.ConstUtil;
import com.chongbangbang.util.HttpClientUtil;
import com.chongbangbang.util.PartitionUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;

/**
 * @author TP
 * @version 1.0
 * @date 2022/8/27/0027 17:42
 */
@RestController
@RequestMapping("/chong/wx")
@Log4j
public class WXController {

    @Autowired
    private WXService wxService;

    @Autowired
    private PetInfoService petInfoService;
    @Autowired
    private UserInfoService userInfoService;


    @ApiOperation("微信登录获取opeenId等信息")
    @GetMapping("/login")
    public String login(String code) {
        String url = ConstUtil.WX_LOGIN_URL + "appid=" + ConstUtil.APPID + "&secret=" +
                ConstUtil.SECRET + "&js_code=" + code + "&&grant_type=" + ConstUtil.GRANT_TYPE;
        String result = HttpClientUtil.doGet(url);
        System.out.println("\n\n" + result + "\n\n");
        return result;
    }

    @ApiOperation("通过openId获取用户数据")
    @GetMapping("/{openId}")
    public UserInfo getUserByOpenId(@PathVariable String openId) throws UnsupportedEncodingException {
        UserInfo userInfo = wxService.getUserByOpenId(openId);
        if (userInfo != null) {
            if (!StringUtils.isEmpty(userInfo.getUsername())) {
                // 出库后，使用 Base64 解码
                String username = new String(Base64.decodeBase64(userInfo.getUsername().getBytes()), "utf-8");
                userInfo.setUsername(username);
            }
        }

        return userInfo;
    }

    @ApiOperation("图片上传到服务器")
    @PostMapping( "/upload")
    public String upload(MultipartFile file, Integer userId) {
        if (file.isEmpty()) {
            log.error("文件为空");
            return "0";
        }

        String fileName = wxService.upload(file, userId + "");
        String fileWebPath = ConstUtil.WEBSITE_IMAGE_PATH + "/" + fileName;
        String databaseFilePath =  fileWebPath + ConstUtil.SEPARATOR;

//        String testFilePath =  ConstUtil.IMAGE_PATH + "/" + fileName;
        if (fileName == null) {
            log.error("文件名为空，文件上传失败");
            return "0";
        }

        // 将图片地址保存到数据库
            // 用户头像
//        if (ConstUtil.PET_ID_USER_IMAGE_PATH.equals(petId)) {
//            UserInfo userInfo = new UserInfo();
//            userInfo.setAvatar(fileWebPath);
//            UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
//            updateWrapper.eq("id", userId);
//            userInfoService.update(userInfo, updateWrapper);
//        } else {
//            PetInfo petInfo = new PetInfo();
//            petInfo.setPetPhoto(fileWebPath);
//            UpdateWrapper<PetInfo> updateWrapper = new UpdateWrapper<>();
//            updateWrapper.eq("id", petId);
//            petInfoService.update(petInfo, updateWrapper);
//        }

        return fileWebPath;
    }
}
